<template>
  <div class="AppShipToAnd">
    <div class="nav-top">
      <NavTopReturn title="添加收货地址"></NavTopReturn>
    </div>

    <div class="content">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        show-search-result
        :area-columns-placeholder="['省市区', '请选择', '请选择']"
        @save="baoCunDiZi"
        @change-detail="onChangeDetail"
        :address-info="adderssInfo"

      />
    </div>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import NavTopReturn from "../components/NavTopReturn.vue";
export default {
  name: "AppShipToAnd",
  components: {
    NavTopReturn,
  },

  data() {
    return {
      // 省市区数据
      areaList,
      //地址得id
      aid: "",
      //联系人初始信息
      adderssInfo:{},

    };
  },

  created() {
    //截取编辑的aid
    this.aid = this.$route.query.aid;
    console.log("this.aid=>", this.aid);
  

    //获取编辑
    this.getAddressData(this.aid);
  },

  methods: {
    //保存新增收货地址
    baoCunDiZi(content) {
      // content是添加的地址
      //获取本地地址
      let bendiaddressData = localStorage.getItem("addressData");

      if (!bendiaddressData) {
        bendiaddressData = [];
      } else {
        bendiaddressData = JSON.parse(bendiaddressData);
      }

      //需要复制的，不能直接引用
      let addressData = { ...content};
      // 在添加新的地址
      bendiaddressData.push(addressData);
     /*  if(bendiaddressData == null){
        bendiaddressData
      }else{
        bendiaddressData.forEach(item => {
          item.aid = new Date().getTime();
          if(!item.isDefault){
            item.isDefault = false
            if(item.areaCode == this.aid){
            item.isDefault = true
            }
          }else{
            item.isDefault = false
            if(item.areaCode == this.aid){
            item.isDefault = true
            }
  
          }
          
        });

      } */
      //去除空的
      delete addressData.country;




      //存储到本地
      window.localStorage.setItem(
        "addressData",
        JSON.stringify(bendiaddressData)
      );

      //提示
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner",
      });

      //跳转地址保存页面
      setTimeout(() => {
        
        this.$router.push({ name: "ShipToAddress" });
      }, 1000);
    },

    //根据ID获取地址数据
    getAddressData(aid){

      //获取本地数据
      let bendiaddressData = JSON.parse(localStorage.getItem("addressData"));

      console.log("bendiaddressData=>>>",bendiaddressData);
      if(bendiaddressData == null){
        bendiaddressData
      }else{
        bendiaddressData.forEach(item=>{
          if(item.areaCode == aid){
            this.adderssInfo = item
          }

  
        })

      }
      console.log("this.adderssInfo==>",this.adderssInfo);

    },

    onSave() {
      Toast("save");
    },
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.AppShipToAnd {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
}
</style>